<?php
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

class bi_template
{

    var $path		= NULL;
    var $mode		= 0;//for path..
    var $name		= NULL;
    
    var $preview	= NULL;
    
    var $_CONF	= NULL;//external
    var $_HTML = NULL;//external

    
    /**
     * @param str $path (SUBDIR)
     * @param str $filename
     * 
     * @param obj $HTML html class
     */
    function bi_template( $path, $filename, $mode )
    {
    	$this->path			= $path;
    	$this->mode			= $mode;
    	$this->name			= $filename;
   
    	$this->_HTML		= new HTML_BIndex;
    	global $biConfig;//			= new biConfig();
    	$this->_CONF		= $biConfig;
    }// function



/**
 * #### this one goes into iframe ###
 * 
 * @param int $mode
 */
	function preview()
	{
		global $database, $mainframe;
##		global $biConfig;
	global $DBG;
	#$DBG->out($template,0,'$template');	
	#$DBG->out($mode,0,'$mode');	
		
		#dummy item
		$Bid = new bindex( $database );
		
	//	$subDir = $this->path;
		
		$Bid->name		= BI_PREVIEW. ' ' . BI_NAME;
		$Bid->stadt = BI_PREVIEW. ' ' . BI_CITY;
		$Bid->strasse = BI_PREVIEW. ' ' . BI_STREET;	
		$Bid->hausnummer = '000';	
		$Bid->telefon = BI_PREVIEW. ' ' . BI_TELEFONE;	
		$Bid->fax = BI_PREVIEW. ' ' . BI_FAX;
		$Bid->email = 'email@example.com';	
		$Bid->bild = 'bi_preview.png';	
		$Bid->beschreibung = BI_PREVIEW. ' ' . BI_DESCRIPTION;
		
		#item to be included in template
		$item = new biItem( $Bid, $Bid);
	
		$link_view = '#';
		$link_edit = '#';
		$link_kill = '#';
		
		$href = $this->_CONF->templatePath.$this->path.$this->name;
	
############################
### include template css ###
		$mainframe->addCustomHeadTag('<link rel="stylesheet" type="text/css" media="all" href="'.$href.'.css" />');
############################
### include template php ###
		include( $href.'.php');
############################
	
	} //function

	function edit ()
	{
		$lists['template_item'] 		= $this->getSelectList( 1, $this->name, TRUE ); //item
		$lists['template_directory'] 	= $this->getSelectList( 2, $this->name, TRUE );
		$lists['template_app'] 			= $this->getSelectList( 3, $this->name, TRUE );

		$this->formTemplate( $lists );
	} //function
    
    
/**
 * @param int $mode - subdir + fieldname
 * 		1 - item
 * 		2 - directory
 * 		3 - app
 * @param str $template - selected template
 * @param bol $submitJS - JavaScript action onchange
 */
	function getSelectList ( $mode, $template = FALSE, $submitJS = FALSE )
	{
//	global $biConfig;
global $DBG;
	
	$ret = NULL;
	$submit_action = '';
	
		$subDir = '';
		$sel_item = '';
		$field_name = '';
		switch( $mode )
		{
			case 1:
				$subDir = 'item/';
				$field_name = '_item';
				$sel_item = ( $template ) ? $template : $this->_CONF->templateItem;
			break;
			case 2:
				$subDir = 'directory/';
				$field_name = '_directory';
				$sel_item = ( $template ) ? $template : $this->_CONF->templateDirectory;
			break;
			case 3:
				$subDir = 'app/';
				$field_name = '_app';
				$sel_item = ( $template ) ? $template : $this->_CONF->templateApp;
			break;
			default:
				echo 'no mode';
				return;
			break;
		} // switch
	
		$templates = array();

	if( $handle = opendir( $this->_CONF->templatePath.$subDir ) )
	{
		$i=0;
		while ($file = readdir($handle)) {
			if( !strcasecmp(substr($file,-4),".php")
				 && $file <> "." && $file <> ".."
				 && strcasecmp(substr($file,-11),"preview.php"))
			{
				$templates[] = mosHTML::makeOption( substr($file,0,-4) );
			} //if
		} //while
	} //if
//$DBG->out($templates);
	# sort list of languages
#	sort($templates);
#	reset($templates);
	if( $submitJS )
	{
		$submit_action = " onchange=\"" .
//				"document.frmSelect.code_php.value=codearea_php.getCode(); " .
//				"document.frmSelect.code_css.value=codearea_css.getCode(); " .
//				"document.frmSelect.preview.value='yes'; " .
				"document.frmSelect.mode.value='".$mode."'; " .
				"document.frmSelect.template.value=document.frmSelect.template".$field_name.".value; " .
				"document.frmSelect.submit();" .
				"\"";
	} //if
	
	$ret = mosHTML::selectList( $templates, 'template'.$field_name, 'class="inputbox" size="1"'.$submit_action, 'value', 'text', $sel_item );
	
	return ($ret);

} //function
    
 	function formTemplate( $lists )
	{
		global $mainframe, $Itemid;
##		global $biConfig;
global $DBG;
		
		//if( $mode == 0 ) $mode = 1;
		
		# include codepress JS
		#$mainframe->addCustomHeadTag('<script src="components/com_bindex/includes/js/codepress/codepress.js" type="text/javascript"></script>');  
		$this->_HTML->load_JS_CodePress();
		$this->_HTML->load_JS_Tabs_Slides();

		$this->_HTML->biHeader(BI_TEMPLATES);

		$subDir = $this->path;
/*
		switch( $mode )
		{
			case 2:
				$subDir = 'directory/';
			break;
			case 3:
				$subDir = 'app/';
			break;
			case 1:
			default:
				$subDir = 'item/';
			break;
		} //switch

*/

		$link_base = 'index.php?option=com_bindex&amp;Itemid='.$Itemid.'&amp;mode='.$this->mode;
		$link_base2 = str_replace('index.php', 'index2.php', $link_base);
		
		$link_save = $link_base . '&amp;task=save_template';
		$link_reload = $link_base.'&amp;task=templates';
		
		$code_php = '';
		$code_css = '';
		$template_name = ($this->name != '' ) ? $this->name : $this->_CONF->templateItem;

		$href_iframe_template = $link_base2.'&amp;task=template_preview&amp;template='.$template_name;
		$href_iframe_preview = $link_base2.'&amp;task=template_preview&amp;template=preview';
				
		$submit_action = 'document.frmCode.code_php.value=codearea_php.getCode();document.frmCode.code_css.value=codearea_css.getCode();document.frmCode.submit();';
		$preview_action = "document.frmCode.preview.value='yes'; ";

		$full_file = $this->_CONF->templatePath.$subDir.$template_name;
		
//echo $full_file;

#$DBG->out($mode);
$DBG->out($full_file);			
#$DBG->out($biConfig->templateItem,0,'$biConfig->templateItem');
		if( $this->preview == 'yes' ) $full_file = $this->_CONF->templatePath.$subDir.'preview';

		################
		# Loading template and css
		
		if( ! file_exists( $full_file . '.php' ) )
		{	$this->_HTML->msgError(BI_NO_TEMPLATE);
			return;
		} else
		{	$code_php = file_get_contents($full_file.'.php');
			$code_php = preg_replace("/>/","&gt;",$code_php);
			$code_php = preg_replace("/</","&lt;",$code_php);
		} //if

		if( ! file_exists( $full_file . '.css' ) )
		{ $this->msgError(BI_NO_CSS);
		} else
		{	$code_css = file_get_contents($full_file.'.css');
			$code_css = preg_replace("/>/","&gt;",$code_css);
			$code_css = preg_replace("/</","&lt;",$code_css);
		} //if
		
		
		$JS_select_list_action = "document.frmSelect.mode.value='%d'; " .
				"document.frmSelect.template.value=document.frmSelect.template%s.value; " .
				"document.frmSelect.submit();";

		?>
		<form action="<?php echo $link_reload; ?>" method="post" name="frmSelect">
			<table width="100%" class="bi_menu">
				<tr>
<!--					
								<td align="left">
									<strong><?php echo $this->_CONF->templatePath.'<br />'.$subDir; ?></strong>
								</td>
-->
									<td <?php if( $this->mode == 1 ) echo ' class="selector_selected"'; ?> width="10%">
									Item<?php echo $lists['template_item']; ?>
									<a href="javascript: <?php printf($JS_select_list_action, 1, '_item'); ?>">Edit</a>
									</td>
									<td <?php if( $this->mode == 2 ) echo ' class="selector_selected"'; ?> width="10%">
									Directory<?php echo $lists['template_directory']; ?>
									<a href="javascript: <?php printf($JS_select_list_action, 2, '_directory'); ?>">Edit</a>
									</td>
									<td <?php if( $this->mode == 3 ) echo ' class="selector_selected"'; ?> width="10%">
									App<?php echo $lists['template_app']; ?>
									<a href="javascript: <?php printf($JS_select_list_action, 3, '_app'); ?>">Edit</a>
		<input type="hidden" name="template" value="" />
		<input type="hidden" name="mode" value="0" />
									</td>
					<?php $this->_HTML->_menuButton( '#', '', 'save.png', BI_SAVE, FALSE, $submit_action ); ?>
						
					<?php $this->_HTML->_menuButton( 'index2.php?option=com_bindex&amp;help=item_defs', 'help', 'help_f2.png', BI_TEMPLATE_DEFINITIONS, TRUE ); ?>
				</tr>
			</table>		
		</form>


		<form action="<?php echo $link_save; ?>" method="post" name="frmCode">

		<div class="jwts_tabber" id="jwts_tab1">

			<div class="jwts_tabbertab" title="">
				<h2><a href="javascript:void(null);" name="advtab"><?php echo BI_TEMPLATE; ?></a></h2>
				<iframe src="<?php echo $href_iframe_template; ?>" width="100%" height="400" name="preview" frameborder="0">
			  		<p><?php echo BI_NO_FRAMES; ?></p>
				</iframe>
			</div>

			<div class="jwts_tabbertab" title="">
				<h2><a href="javascript:void(null);" name="advtab"><?php echo BI_PREVIEW; ?></a></h2>


				<?php
				if( $this->preview == 'yes' )
				{ ?>
					<iframe src="<?php echo $href_iframe_preview; ?>" width="100%" height="400" name="preview" frameborder="0">
					  <p><?php echo BI_NO_FRAMES; ?></p>
					</iframe>
					<?php
				} //if
				?>
				<input type="button" class="bi_menu_button_selected" value="<?php echo BI_PREVIEW_UPDATE; ?>" onclick="<?php echo $preview_action.$submit_action; ?>" />

			</div>

	
		</div>   <!-- close tabber -->

		<?php
		if( $code_php != '' )
		{
			?>

				<div class="bi_menu">
					<img src="components/com_bindex/images/html_f2.png" alt="templates" border="0" />
					<span style="font-size: 1.4em;">PHP</span>
				</div>
				<link type="text/css" href="components/com_bindex/includes/js/codepress/languages/php.css" rel="stylesheet" />
				<textarea style="width:100%;" rows="20" cols="80" id="codearea_php" name="codearea_php" class="codepress php linenumbers-on"><?php echo $code_php; ?></textarea>
			<?php
		} //if


		if( $code_css != '' )
		{
			?>

				<div class="bi_menu">
					<img src="components/com_bindex/images/css_f2.png" alt="templates" border="0" />
					<span style="font-size: 1.4em;">CSS</span>
				</div>
				<link type="text/css" href="components/com_bindex/includes/js/codepress/languages/css.css" rel="stylesheet" />
				<textarea style="width:100%;" rows="20" cols="80" id="codearea_css" name="codearea_css" class="codepress css linenumbers-on"><?php echo $code_css; ?></textarea>
			<?php
		} //if

?>
		<input type="hidden" name="template" value="" />
		<input type="hidden" name="preview" value="" /> 
		<input type="hidden" name="mode" value="<?php echo $this->mode; ?>" />
		<input type="hidden" name="code_php" value="" />
		<input type="hidden" name="code_css" value="" />

	</form>

<!-- <input type="button" value="+" onclick="codearea_php.rows=100;" />
			<input type="button" value="<?php echo BI_SAVE; ?>" onclick="<?php echo $submit_action; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="button" value="<?php echo BI_PREVIEW; ?>" onclick="<?php echo $preview_action.$submit_action; ?>" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-->
<!--

self.resize = function (x, y) { 
self.style.height = (self.clientHeight + y) + "px"; 
self.style.width = (self.clientWidth + x) + "px"; 
} 

			<input type="reset" value="reset.." />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		
			 <input type="submit" name="submit" class="button" value="<?php echo BI_SAVE; ?>" /> -->

<!--
		<strong><a href="javascript: alert(document.forms.adminForm.ieedt.cols);">edit</a></strong>
		
		<script>
		function editorWidthPlus(name)
		{
			document.getElementById(name).cols = 90	
			//name.cols = 90
		
		}
		</script>
		<a href="javascript: editorWidthPlus('ieedt');">jj</a>
-->
		<?php
	} //function

    
}// class



/*

class xxxbiTemplate
{
	function savefile($file_name)
	{			// save edited file
		$biConfig = new biConfig;
	if( get_magic_quotes_gpc() ) {
		$code_php = stripslashes($_POST['code_php']);
	}
	else {
		$code_php = $_POST['code_php'];
	}
	
	$res = file_put_contents( $biConfig->templatePath.$file_name.'.php', $code_php );
	
	if( $res==false ) return FALSE;//|| PEAR::isError( $res )) {
/*
		$err = basename($file_name).": ".$GLOBALS["error_msg"]["savefile"];
		if( PEAR::isError( $res ) ) {
			$err .= $res->getMessage();
		}
		show_error( $err );
	}

	}//function
	

} //class
*/
?>